--[[
    Longer Trains 0.1
    Copyright (C) 2011 Dzakub

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
]]

g_Root = getRootElement()
g_Local = getLocalPlayer()
local gui = {}
gui._placeHolders = {}
local screenWidth, screenHeight = guiGetScreenSize()
local windowWidth, windowHeight = 155, 248
local left = screenWidth/2 - windowWidth/2
local top = screenHeight/2 - windowHeight/2
gui["_root"] = guiCreateWindow(left, top, windowWidth, windowHeight, "Longer Trains", false)
guiWindowSetSizable(gui["_root"], false)

gui["label"] = guiCreateLabel(10, 225, 191, 16, "Longer Trains by Dzakub", false, gui["_root"])
guiLabelSetHorizontalAlign(gui["label"], "left", false)
guiLabelSetVerticalAlign(gui["label"], "center")

gui["streak"] = guiCreateRadioButton(20, 55, 121, 17, "Pasazerski (Streak)", false, gui["_root"])

gui["label_2"] = guiCreateLabel(20, 35, 141, 16, "Wybierz rodzaj pociagu:", false, gui["_root"])
guiLabelSetHorizontalAlign(gui["label_2"], "left", false)
guiLabelSetVerticalAlign(gui["label_2"], "center")

gui["freight"] = guiCreateRadioButton(20, 75, 121, 17, "Towarowy (Freight)", false, gui["_root"])

gui["createTrain"] = guiCreateButton(20, 95, 111, 23, "Stworz pociag", false, gui["_root"])

gui["destroyTrain"] = guiCreateButton(20, 125, 111, 23, "Usun pociag", false, gui["_root"])

gui["hasTrain"] = guiCreateLabel(20, 155, 121, 16, "Nie posiadasz pociagu.", false, gui["_root"])
guiLabelSetHorizontalAlign(gui["label_3"], "center", false)
guiLabelSetVerticalAlign(gui["label_3"], "center")

gui["pushButton_3"] = guiCreateButton(131, 15, 20, 20, "x", false, gui["_root"])
guiSetVisible(gui["_root"], false)
setElementData(g_Local, "hasTrain", false)
setElementData(g_Local, "train", nil)

function showHideWindow()
	if guiGetVisible(gui["_root"]) then
		guiSetVisible(gui["_root"], false)
		refreshOwnership()
	else
		guiSetVisible(gui["_root"], true)
		refreshOwnership()
	end
end

addEvent("onPlayerEnterTrainMarker", true)
addEventHandler("onPlayerEnterTrainMarker", g_Root, showHideWindow)

function refreshOwnership()
	if getElementData(g_Local, "hasTrain") == true then
		guiSetText(gui["hasTrain"], "Posiadasz pociag.")
		guiSetEnabled ( gui["createTrain"], false )
		guiSetEnabled ( gui["destroyTrain"], true )
	else
		guiSetText(gui["hasTrain"], "Nie posiadasz pociagu.")
		guiSetEnabled ( gui["createTrain"], true )
		guiSetEnabled ( gui["destroyTrain"], false )
	end
end

function handleButtons()

end


for k,v in pairs(gui) do
	if getElementType == "gui-button" then
		addEventHandler()
	end
end

